/* ictrancn.p - ISSUE OF CONSIGNED PO MATERIAL                              */
/* Copyright 1986-2003 QAD Inc., Carpinteria, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.             */
/* $Revision: 1.26.1.4 $                                                            */

/* Revision: 1.21     BY: Patrick Rowan         DATE: 08/09/01  ECO: *P017* */
/* Revision: 1.22     BY: Patrick Rowan         DATE: 06/19/01  ECO: *P091* */
/* Revision: 1.23     BY: Reetu Kapoor          DATE: 07/26/02  ECO: *P0CB* */
/* Revision: 1.24     BY: Patrick Rowan         DATE: 08/15/02  ECO: *P0FQ* */
/* Revision: 1.25     BY: Katie Hilbert         DATE: 01/06/03  ECO: *P0LN* */
/* Revision: 1.26     BY: Rajiv Ramaiah         DATE: 03/20/03  ECO: *P0NY* */
/* Revision: 1.26.1.1 BY: Gnanasekar            DATE: 06/27/03  ECO: *P0WB* */
/* Revision: 1.26.1.2 BY: Nishit V              DATE: 07/14/03  ECO: *P0X5* */
/* Revision: 1.26.1.3 BY: Katie Hilbert         DATE: 08/22/03  ECO: *P10L* */
/* $Revision: 1.26.1.4 $      BY: Dipesh Bector         DATE: 08/31/03  ECO: *P11Z* */

/*V8:ConvertMode=NoConvert                                                  */

/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */
/*                                                                          */
/*         THIS PROGRAM IS A BOLT-ON TO STANDARD PRODUCT MFG/PRO.           */
/* ANY CHANGES TO THIS PROGRAM MUST BE PLACED ON A SEPARATE ECO THAN        */
/* STANDARD PRODUCT CHANGES.  FAILURE TO DO THIS CAUSES THE BOLT-ON CODE TO */
/* BE COMBINED WITH STANDARD PRODUCT AND RESULTS IN PATCH DELIVERY FAILURES.*/
/*                                                                          */
/* -----  W A R N I N G  -----  W A R N I N G  -----  W A R N I N G  -----  */

{mfdeclre.i}
{pxmaint.i}
{apconsdf.i}

/* PARAMETERS */
define input parameter ip_wo_nbr      like wo_nbr       no-undo.
define input parameter ip_wo_lot      like wo_lot       no-undo.
define input parameter ip_wod_op      like wod_op       no-undo.
define input parameter ip_wo_so_job   like wo_so_job    no-undo.
define input parameter ip_qty_req     like tr_qty_chg   no-undo.
define input parameter ip_lotser      like sod_serial   no-undo.
define input parameter ip_part        like pt_part      no-undo.
define input parameter ip_site        like si_site      no-undo.
define input parameter ip_loc         like pt_loc       no-undo.
define input parameter ip_lotref      like sr_ref       no-undo.
define input parameter ip_effdate     like glt_effdate  no-undo.
define input parameter ip_trnbr       like tr_trnbr     no-undo.
define input parameter ip_wo_issue    as logical        no-undo.
define input-output
             parameter io_batch       like cnsu_batch   no-undo.

/* SHARED VARIABLES */

{porcdef.i "new" "new"}

define  new   shared workfile tax_wkfl
   field tax_nbr         like pod_nbr
   field tax_line        like pod_line
   field tax_env         like pod_tax_env
   field tax_usage       like pod_tax_usage
   field tax_taxc        like pod_taxc
   field tax_in          like pod_tax_in
   field tax_taxable     like pod_taxable
   field tax_price       like prh_pur_cost.

define  new   shared variable rndmthd             like rnd_rnd_mthd.
define  new   shared variable vendlot             like tr_vend_lot  no-undo.
define  new   shared variable qopen               like pod_qty_rcvd.
define  new   shared variable receipt_date        like prh_rcp_date no-undo.
define  new   shared variable receivernbr         like prh_receiver.
define  new   shared variable fiscal_rec          as   logical initial false.
define  new   shared variable kbtransnbr          as integer no-undo.
define  new   shared variable prm-avail           like mfc_logical no-undo.
define  new   shared variable h_wiplottrace_procs as handle no-undo.
define  new   shared variable h_wiplottrace_funcs as handle no-undo.
define  new   shared variable old_db              like si_db.
define  new   shared variable new_db              like si_db.

/*WORKFILE FOR POD RECEIPT ATTRIBUTES*/
define new shared workfile attr_wkfl no-undo
   field chg_line    like sr_lineid
   field chg_assay   like tr_assay
   field chg_grade   like tr_grade
   field chg_expire  like tr_expire
   field chg_status  like tr_status
   field assay_actv  as   logical
   field grade_actv  as   logical
   field expire_actv as   logical
   field status_actv as   logical.


/* VARIABLES */
define variable process_consign_now  like cns_consign_flag initial no no-undo.
define variable end_of_consigned_inv like mfc_logical      initial no no-undo.
define variable end_of_normal_inv    like mfc_logical      initial no no-undo.
define variable remaining_xfer_qty   like tr_qty_req                  no-undo.
define variable offset_acct          like trgl_dr_acct                no-undo.
define variable offset_sub           like trgl_dr_sub                 no-undo.
define variable offset_cc            like trgl_dr_cc                  no-undo.
define variable cons_inv_acct        like trgl_cr_acct                no-undo.
define variable cons_inv_sub         like trgl_cr_sub                 no-undo.
define variable cons_inv_cc          like trgl_cr_cc                  no-undo.
define variable inv_acct             like trgl_dr_acct                no-undo.
define variable inv_sub              like trgl_dr_sub                 no-undo.
define variable inv_cc               like trgl_dr_cc                  no-undo.
define variable rcpt_acct            like trgl_cr_acct                no-undo.
define variable rcpt_sub             like trgl_cr_sub                 no-undo.
define variable rcpt_cc              like trgl_cr_cc                  no-undo.
define variable gl_amt               like trgl_gl_amt                 no-undo.
define variable cum_qty              like cnsu_cum_qty                no-undo.
define variable lotser_from          like sr_lotser                   no-undo.
define variable lotser_to            like sr_lotser                   no-undo.
define variable mc-error-number      like msg_nbr                     no-undo.
define variable gl_tmp_amt           as decimal                       no-undo.
define variable msg-num              like tr_msg                      no-undo.
define variable price                like tr_price                    no-undo.
define variable amt                  as decimal                       no-undo.
define variable proc_id              as character                     no-undo.
define variable ip_accrue_tax        like mfc_logical                 no-undo.
define variable ip_pod_project       like pod_project                 no-undo.
define variable op_cniss_trnbr       like tr_trnbr                    no-undo.
define variable op_rctpo_trnbr       like tr_trnbr                    no-undo.
define variable auto_receipt         as logical        initial false  no-undo.
define variable CONSIGNMENT_FIRST    like cns_picking_logic initial 1 no-undo.
define variable err_flag             like mfc_logical                 no-undo.
define variable glx_mthd             like cs_method                   no-undo.
define variable cur_mthd             like cs_method                   no-undo.
define variable glx_set              like cs_set                      no-undo.
define variable cur_set              like cs_set                      no-undo.


/* TEMP-TABLES */
define new shared temp-table tt_exru_usage     like exru_usage.
define            temp-table tt_pvo_mstr       no-undo like pvo_mstr.

{pocnvars.i}

eff_date = ip_effdate.

for first gl_ctrl no-lock:
end. /*For first gl_ctrl*/

/* STRIP OUT LOTSER FROM/TO FROM lotserial */
lotser_from = right-trim(substring(ip_lotser,1,18)).
if right-trim(substring(ip_lotser,40,1)) = "#" then
   lotser_to = "".
else
if right-trim(substring(ip_lotser,19)) = ""
then
   lotser_to = lotser_from.
else
   lotser_to = right-trim(substring(ip_lotser,19,18)).

/* DETERMINE IF THE LOCATION CONTAINS SUPPLIER CONSIGNED INVENTORY. */
/* IF "YES" THEN CONTINUE, OTHERWISE RETURN TO CALLING PROGRAM.     */

if can-find(first ld_det where
               ld_part    = ip_part      and
               ld_site    = ip_site      and
               ld_loc     = ip_loc       and
               ld_lot     = lotser_from  and
               ld_ref     = ip_lotref    and
               ld_supp_consign_qty = 0)
   then
      return.

/* READ CONTROL FILE */
for first cns_ctrl
    where cns_active
no-lock:
   if cns_picking_logic = CONSIGNMENT_FIRST then
      process_consign_now = yes.
end.

/* DETERMINE COSTING METHOD */
{gprun.i ""csavg01.p"" "(input  ip_part,
                         input  ip_site,
                         output glx_set,
                         output glx_mthd,
                         output cur_set,
                         output cur_mthd)" }

/* INITIALIZE WORKING QTY */
remaining_xfer_qty = ip_qty_req.

/* POSITIVE QUANTITY */
if remaining_xfer_qty > 0 then do:

   repeat while remaining_xfer_qty > 0:

      /* IF END OF BOTH TYPES OF INVENTORY THEN JUST QUIT */
      if end_of_consigned_inv and end_of_normal_inv then leave.

      if process_consign_now then do:

         /* IF USE CONSIGNED 1st THEN                 */
         /* 1. FOR EACH receipt x-reference           */
         /* 2. Deduct qty                             */
         /* 3. Create tr_hist                         */
         /* 4. Create usage record                    */
         /* 5. Create pending voucher record          */
         /* 6. Calculate PPV                          */
         /* 7. Accrue Taxes                           */
         /* 8. Increase CUM Consumed                  */

         for each cnsix_mstr where
               cnsix_part    = ip_part     and
               cnsix_site    = ip_site     and
               cnsix_lotser  = lotser_from and
               cnsix_ref    = ip_lotref    and
               cnsix_qty_consigned > 0
            exclusive-lock,
            first prh_hist where
                  prh_nbr = cnsix_po_nbr    and
                  prh_line = cnsix_pod_line and
                  prh_receiver = cnsix_receiver
               no-lock
            break by cnsix_pkey:

            price = prh_pur_cost.

            /*GET CONSIGNMENT ACCOUNT INFORMATION */
            {gprunmo.i
               &program = ""pocnacct.p""
               &module  = "ACN"
               &param   = """(input ip_part,
                              input ip_site,
                              input prh_vend,
                              output cons_inv_acct,
                              output cons_inv_sub,
                              output cons_inv_cc,
                              output offset_acct,
                              output offset_sub,
                              output offset_cc)"""}

            /*GET INVENTORY AND RECEIPT ACCOUNTS*/
            run getAccounts
               (input ip_part,
                input ip_site,
                input ip_loc,
                output inv_acct,
                output inv_sub,
                output inv_cc,
                output rcpt_acct,
                output rcpt_sub,
                output rcpt_cc).

            /*DETERMINE TAX SETTINGS*/
            for first tx2d_det no-lock
               where tx2d_ref = cnsix_receiver  and
                     tx2d_nbr = cnsix_po_nbr    and
                     tx2d_line = cnsix_pod_line and
                     tx2d_tr_type = "21":
            end. /*For first tx2d_det*/

            for first tx2_mstr no-lock where
               tx2_tax_code = tx2d_tax_code:

               if tx2_usage_tax_point then
                  ip_accrue_tax = yes.
               else
                  ip_accrue_tax = no.
            end.

            for first po_mstr
               where po_nbr = cnsix_po_nbr
            no-lock:
               po_recno = recid(po_mstr).
            end. /*For first po_mstr*/

            /*GET POD PROJECT AND ENTITY DATA*/
            for first pod_det
               where pod_nbr  = cnsix_po_nbr
                 and pod_line = cnsix_pod_line
            no-lock:
               pod_recno = recid(pod_det).
            end. /*For first pod_det*/
            ip_pod_project = pod_project.

            /*INITIALIZE ATTRIBUTE VARIABLES WITH CURRENT SETTINGS*/
            find first attr_wkfl
               where chg_line = string(pod_line)
            exclusive-lock no-error.

            if not available attr_wkfl then do:
               create attr_wkfl.
               chg_line = string(pod_line).
               if recid(attr_wkfl) = -1 then.
            end. /* IF NOT AVAILABLE ATTR_WKFL */

            {pxrun.i &PROC='initializeAttributes' &PROGRAM='porcxr1.p'
                     &PARAM="(buffer pod_det,
                              input  eff_date,
                              output chg_assay,
                              output chg_grade,
                              output chg_expire,
                              output chg_status,
                              output assay_actv,
                              output grade_actv,
                              output expire_actv,
                              output status_actv,
                              output err_flag)"
                     &NOAPPERROR=true
                     &CATCHERROR=true}

            /* FIND EXCH RATE IF CURRENCY NOT BASE */
            exch_ratetype = po_ex_ratetype.
            if base_curr <> po_curr then do:
               if po_fix_rate then do:
                  assign
                     exch_rate  = po_ex_rate
                     exch_rate2 = po_ex_rate2.

                  {gprunp.i "mcpl" "p" "mc-copy-ex-rate-usage"
                     "(input po_exru_seq,
                       output exch_exru_seq)"}
               end.
               else do:  /*IF NOT FIXED RATE ALLOW FOR SPOT RATE*/

                  /* CREATE EXCHANGE RATE USAGE */

                  {gprunp.i "mcpl" "p" "mc-create-ex-rate-usage"
                     "(input po_curr,
                       input base_curr,
                       input exch_ratetype,
                       input eff_date,
                       output exch_rate,
                       output exch_rate2,
                       output exch_exru_seq,
                       output mc-error-number)"}
               end. /*NOT FIXED RATE*/
            end. /*IF BASE_CURR <> PO_CURR*/
            else
               assign
                  exch_rate     = 1.0
                  exch_rate2    = 1.0
                  exch_exru_seq = 0.


            /* EQUAL QUANTITIES */
            if cnsix_qty_consigned = remaining_xfer_qty then do:

               amt = remaining_xfer_qty * price.

               /* ROUND PER BASE CURRENCY ROUND METHOD */
               {gprunp.i "mcpl" "p" "mc-curr-rnd"
                  "(input-output amt,
                    input gl_rnd_mthd,
                    output mc-error-number)"}

               /*RECEIPT PO */
               receivernbr = cnsix_receiver.

               create sr_wkfl.
               assign
                  sr_userid   = mfguser + "cons"
                  sr_lineid   = string(cnsix_pod_line)
                  sr_site     = cnsix_site
                  sr_loc      = ip_loc
                  sr_lotser   = cnsix_lotser
                  sr_vend_lot = ""
                  sr_ref      = cnsix_ref
                  sr_qty      = remaining_xfer_qty.

               if recid(sr_wkfl) = -1 then .

               {gprun.i ""poporcb.p""
                  "(input """",
                    input ?,
                    input "yes",
                    input po_ship,
                    input auto_receipt,
                    input yes,
                    input sr_qty,
                    input string(ip_trnbr),
                    output op_rctpo_trnbr)"}

               /* CALCULATE PPV */
               /* ACCRUE TAXES */
               /* ADDED ELEVENTH INPUT PARAMETER TO PASS COSTING METHOD */
               {gprunmo.i
                  &program = ""wocnppv.p""
                  &module  = "ACN"
                  &param   = """(input cnsix_po_nbr,
                                 input ip_part,
                                 input ip_site,
                                 input remaining_xfer_qty,
                                 input ip_pod_project,
                                 input prh_pur_cost,
                                 input op_rctpo_trnbr,
                                 input ip_accrue_tax,
                                 input cnsix_receiver,
                                 input cnsix_pod_line,
                                 input glx_mthd)"""}

               /* CREATE USAGE tr_hist */
               run createConsignIssueTransaction
                   (input ip_wo_nbr,
                    input ip_wo_lot,
                    input ip_part,
                    input ip_wod_op,
                    input ip_wo_so_job,
                    input ip_site,
                    input ip_loc,
                    input lotser_from,
                    input ip_lotref,
                    input ip_qty_req,
                    input - remaining_xfer_qty,
                    input offset_acct,
                    input offset_sub,
                    input offset_cc,
                    input cons_inv_acct,
                    input cons_inv_sub,
                    input cons_inv_cc,
                    input ip_effdate,
                    input prh_pur_cost,
                    input "CN-ISS",
                    input amt,
                    input ip_trnbr,
                    output op_cniss_trnbr).

               /* TIME TO CONSUME */
               /* ADJUST CUM CONSUMED */
               run adjustCumConsumed
                  (input remaining_xfer_qty,
                   output cum_qty).

               /* CREATE USAGE RECORDS */
               run createUsageRecords
                  (input remaining_xfer_qty,
                   input ip_trnbr,
                   input op_cniss_trnbr,
                   input cum_qty,
                   input-output io_batch).

               /* CREATE PENDING VOUCHER RECORDS */
               run createPendingVoucherRecords
                  (input remaining_xfer_qty,
                   input ip_effdate,
                   input io_batch).

               /* UPDATE CONSIGNMENT INVENTORY BALANCES */
               run updateConsignmentInventory
                  (input remaining_xfer_qty,
                   input ip_part,
                   input ip_site,
                   input ip_loc,
                   input lotser_from,
                   input ip_lotref).

               /* DELETE X-REFERENCE  */
               delete cnsix_mstr.

               /* CALCULATE REMAINING TRANSFER QTY */
               remaining_xfer_qty = 0.

               /* ALL DONE! */
               leave.
            end.  /* if cnsix_qty_consigned = remaining_xfer_qty */

            /* PARTIAL QUANTITIES */

            if cnsix_qty_consigned > remaining_xfer_qty then do:

                amt = remaining_xfer_qty * price.

                /* ROUND PER BASE CURRENCY ROUND METHOD */
                {gprunp.i "mcpl" "p" "mc-curr-rnd"
                  "(input-output amt,
                    input gl_rnd_mthd,
                    output mc-error-number)"}

               /*RECEIPT PO */

               receivernbr = cnsix_receiver.

               create sr_wkfl.
               assign
                  sr_userid   = mfguser + "cons"
                  sr_lineid   = string(cnsix_pod_line)
                  sr_site     = cnsix_site
                  sr_loc      = ip_loc
                  sr_lotser   = cnsix_lotser
                  sr_vend_lot = ""
                  sr_ref      = cnsix_ref
                  sr_qty      = remaining_xfer_qty.

               if recid(sr_wkfl) = -1 then .

               {gprun.i ""poporcb.p""
                         "(input """",
                           input ?,
                           input "yes",
                           input po_ship,
                           input auto_receipt,
                           input yes,
                           input sr_qty,
                           input string(ip_trnbr),
                           output op_rctpo_trnbr)"}


               /* CALCULATE PPV */
               /* ACCRUE TAXES */
               /* ADDED ELEVENTH INPUT PARAMETER TO PASS COSTING METHOD */
               {gprunmo.i
                  &program = ""wocnppv.p""
                  &module  = "ACN"
                  &param   = """(input cnsix_po_nbr,
                                 input ip_part,
                                 input ip_site,
                                 input remaining_xfer_qty,
                                 input ip_pod_project,
                                 input prh_pur_cost,
                                 input op_rctpo_trnbr,
                                 input ip_accrue_tax,
                                 input cnsix_receiver,
                                 input cnsix_pod_line,
                                 input glx_mthd)"""}

               /* CREATE USAGE tr_hist */
               run createConsignIssueTransaction
                  (input ip_wo_nbr,
                   input ip_wo_lot,
                   input ip_part,
                   input ip_wod_op,
                   input ip_wo_so_job,
                   input ip_site,
                   input ip_loc,
                   input lotser_from,
                   input ip_lotref,
                   input ip_qty_req,
                   input - remaining_xfer_qty,
                   input offset_acct,
                   input offset_sub,
                   input offset_cc,
                   input cons_inv_acct,
                   input cons_inv_sub,
                   input cons_inv_cc,
                   input ip_effdate,
                   input prh_pur_cost,
                   input "CN-ISS",
                   input amt,
                   input ip_trnbr,
                   output op_cniss_trnbr).

               /* TIME TO CONSUME */

               /* ADJUST CUM CONSUMED */
               run adjustCumConsumed
                  (input remaining_xfer_qty,
                   output cum_qty).

               /* CREATE USAGE RECORDS */
               run createUsageRecords
                  (input remaining_xfer_qty,
                   input ip_trnbr,
                   input op_cniss_trnbr,
                   input cum_qty,
                   input-output io_batch).

               /* CREATE PENDING VOUCHER RECORDS */
               run createPendingVoucherRecords
                  (input remaining_xfer_qty,
                   input ip_effdate,
                   input io_batch).

               /* UPDATE CONSIGNMENT INVENTORY BALANCES */
               run updateConsignmentInventory
                  (input remaining_xfer_qty,
                   input ip_part,
                   input ip_site,
                   input ip_loc,
                   input lotser_from,
                   input ip_lotref).

               /* CALCULATE REMAINING TRANSFER QTY */
               assign
                  cnsix_qty_consigned =cnsix_qty_consigned - remaining_xfer_qty
                  remaining_xfer_qty = 0.

               /* ALL DONE! */
               leave.
            end.  /* if cnsix_qty_consigned > remaining_xfer_qty */

            /* EXCESS QUANTITIES */

            if remaining_xfer_qty > cnsix_qty_consigned then do:

                amt = cnsix_qty_consigned * price.

                /* ROUND PER BASE CURRENCY ROUND METHOD */
                {gprunp.i "mcpl" "p" "mc-curr-rnd"
                  "(input-output amt,
                    input gl_rnd_mthd,
                    output mc-error-number)"}

               /*RECEIPT PO */

               receivernbr = cnsix_receiver.

               create sr_wkfl.
               assign
                  sr_userid   = mfguser + "cons"
                  sr_lineid   = string(cnsix_pod_line)
                  sr_site     = cnsix_site
                  sr_loc      = ip_loc
                  sr_lotser   = cnsix_lotser
                  sr_vend_lot = ""
                  sr_ref      = cnsix_ref
                  sr_qty      = cnsix_qty_consigned.
               if recid(sr_wkfl) = -1 then .

               {gprun.i ""poporcb.p""
                         "(input """",
                           input ?,
                           input "yes",
                           input po_ship,
                           input auto_receipt,
                           input yes,
                           input sr_qty,
                           input string(ip_trnbr),
                           output op_rctpo_trnbr)"}

               /* CALCULATE PPV */
               /* ACCRUE TAXES */
               /* ADDED ELEVENTH INPUT PARAMETER TO PASS COSTING METHOD */
               {gprunmo.i
                  &program = ""wocnppv.p""
                  &module  = "ACN"
                  &param   = """(input cnsix_po_nbr,
                                 input ip_part,
                                 input ip_site,
                                 input remaining_xfer_qty,
                                 input ip_pod_project,
                                 input prh_pur_cost,
                                 input op_rctpo_trnbr,
                                 input ip_accrue_tax,
                                 input cnsix_receiver,
                                 input cnsix_pod_line,
                                 input glx_mthd)"""}

               /* CREATE USAGE tr_hist */
               run createConsignIssueTransaction
                  (input ip_wo_nbr,
                   input ip_wo_lot,
                   input ip_part,
                   input ip_wod_op,
                   input ip_wo_so_job,
                   input ip_site,
                   input ip_loc,
                   input lotser_from,
                   input ip_lotref,
                   input ip_qty_req,
                   input - cnsix_qty_consigned,
                   input offset_acct,
                   input offset_sub,
                   input offset_cc,
                   input cons_inv_acct,
                   input cons_inv_sub,
                   input cons_inv_cc,
                   input ip_effdate,
                   input prh_pur_cost,
                   input "CN-ISS",
                   input amt,
                   input ip_trnbr,
                   output op_cniss_trnbr).

               /* TIME TO CONSUME */

               /* ADJUST CUM CONSUMED */
               run adjustCumConsumed
                  (input cnsix_qty_consigned,
                   output cum_qty).

               /* CREATE USAGE RECORDS */
               run createUsageRecords
                  (input cnsix_qty_consigned,
                   input ip_trnbr,
                   input op_cniss_trnbr,
                   input cum_qty,
                   input-output io_batch).

               /* CREATE PENDING VOUCHER RECORDS */
               run createPendingVoucherRecords
                  (input cnsix_qty_consigned,
                   input ip_effdate,
                   input io_batch).

               /* UPDATE CONSIGNMENT INVENTORY BALANCES */
               run updateConsignmentInventory
                  (input cnsix_qty_consigned,
                   input ip_part,
                   input ip_site,
                   input ip_loc,
                   input lotser_from,
                   input ip_lotref).

               /* CALCULATE REMAINING TRANSFER QTY */
               remaining_xfer_qty = remaining_xfer_qty - cnsix_qty_consigned.

               /* DELETE X-REFERENCE  */
               delete cnsix_mstr.

            end.  /* if cnsix_qty_consigned > remaining_xfer_qty */

         end.  /* for each cnsix_mstr */

         /* IF THERE IS QUANTITY REMAINING, THEN SET THE  */
         /* PROCESS FLAG TO PROCESS NON-CONSIGN MATERIAL  */
         if remaining_xfer_qty > 0 then
            process_consign_now = no.

         end_of_consigned_inv = yes.

      end.  /* if process_consign_now */
      else do:

         /* IF USE CONSIGNED LAST OR PICKING ORDER IS */
         /* SET TO DEFAULT THEN LOOK AT UNCONSIGNED   */
         /* INVENTORY FIRST.                          */

         for each ld_det where
               ld_part    = ip_part      and
               ld_site    = ip_site      and
               ld_loc     = ip_loc       and
               ld_lot     = lotser_from  and
               ld_ref     = ip_lotref
         no-lock:

            /* CALCULATE REMAINING TRANSFER QTY */
            if ((ld_qty_oh + remaining_xfer_qty) - ld_supp_consign_qty) > 0
            then

               /* IF THERE IS MORE NON-CONSIGNED QUANTITY, THEN REDUCE  */
               /* THE REMAINING QUANTITY BY THE NON-CONSIGNED QUANTITY: */
               /* THE DIFFERENCE OF THE TOTAL - CONSIGNED QUANTITIES.   */
               remaining_xfer_qty = remaining_xfer_qty -
                  ((ld_qty_oh + remaining_xfer_qty) - ld_supp_consign_qty).
            else
               if (ld_qty_oh - ld_supp_consign_qty) < 0
               then

                  /* IF THERE IS MORE CONSIGNED QUANTITY (BECAUSE THE ISS- */
                  /* HAS ALREADY REDUCED THE ld_qty_oh) THEN DETERMINE THE */
                  /* REMAINING QUANTITY BY SUBTRACTING THE SUPPLIER QTY    */
                  /* FROM THE REMAINING QTY.  THIS IS THE PORTION OF THE   */
                  /* SUPPLIER QUANTITY REMAINING TO BE PROCESSED.          */
                  /* CALCULATE REMAINING TRANSFER QTY */

                  remaining_xfer_qty = ld_supp_consign_qty - ld_qty_oh.
               else
                  remaining_xfer_qty = 0.

            /* ALL DONE! */
            if remaining_xfer_qty <= 0
            then
               leave.

         end.  /* FOR EACH ld_det */

         /* IF THERE IS QUANTITY REMAINING, THEN SET THE  */
         /* PROCESS FLAG TO PROCESS CONSIGNED MATERIAL    */
         if remaining_xfer_qty > 0 then
            process_consign_now = yes.

         end_of_normal_inv = yes.

      end.  /* else do: if process_consign_now */
   end.  /* repeat while remaining_xfer_qty > 0 */
end.  /* if remaining_xfer_qty > 0 */


/* ========================================================================= */
/* ************************ INTERNAL PROCEDURES **************************** */
/* ========================================================================= */

/* ========================================================================= */
PROCEDURE adjustCumConsumed:
/* ------------------------------------------------------------------------- */
define input parameter in_qty_consumed like remaining_xfer_qty no-undo.
define output parameter out_cum_qty    like cnsu_cum_qty       no-undo.

for first pod_det
    field (pod_cum_qty[4])
    where pod_nbr = cnsix_mstr.cnsix_po_nbr and
          pod_line = cnsix_mstr.cnsix_pod_line
    exclusive-lock:
      assign
         out_cum_qty = pod_cum_qty[4]
         pod_cum_qty[4] = pod_cum_qty[4] + in_qty_consumed.
end.  /* for first pod_det */
END PROCEDURE.  /* Procedure adjustCumConsumed */


/* ========================================================================= */
PROCEDURE createUsageRecords:
/* ------------------------------------------------------------------------- */
define input  parameter in_qty_consumed like remaining_xfer_qty no-undo.
define input  parameter in_woiss_trnbr  like tr_trnbr           no-undo.
define input  parameter in_cniss_trnbr  like tr_trnbr           no-undo.
define input  parameter in_cum_qty      like cnsu_cum_qty       no-undo.
define input-output parameter io_batch  like cnsu_batch         no-undo.

define variable v_cnsu_pkey like cnsu_pkey no-undo.

/* CREATE USAGE MASTER: cnsu_mstr  */
run createUsageMaster
   (input in_qty_consumed,
    input in_cum_qty,
    input in_woiss_trnbr,
    input-output io_batch,
    output v_cnsu_pkey).

/* CREATE USAGE DETAIL: cnsud_det  */
run createUsageDetail
   (input in_qty_consumed,
    input v_cnsu_pkey).

END PROCEDURE.  /* Procedure createUsageRecords */


/* ========================================================================= */
PROCEDURE createUsageMaster:
/* ------------------------------------------------------------------------- */
define input  parameter in_qty_consumed like remaining_xfer_qty no-undo.
define input  parameter in_cum_qty      like cnsu_cum_qty       no-undo.
define input  parameter in_woiss_trnbr  like tr_trnbr           no-undo.
define input-output
              parameter io_batch        like cnsu_batch         no-undo.
define output parameter out_pkey        like cnsu_pkey          no-undo.

/* DETERMINE WHETHER TO USE AN EXISTING BATCH NUMBER OR CREATE A NEW ONE */
if io_batch <> 0 then do:
   for first cnsu_mstr where
        cnsu_po_nbr   = cnsix_mstr.cnsix_po_nbr   and
        cnsu_pod_line = cnsix_mstr.cnsix_pod_line and
        cnsu_trnbr    = in_woiss_trnbr
      exclusive-lock:

         /* ADD TO AN EXISTING USAGE RECORD */
         assign
            cnsu_qty_used = cnsu_qty_used + in_qty_consumed
            cnsu_cum_qty  = cnsu_cum_qty + in_qty_consumed
            io_batch      = cnsu_batch
            out_pkey      = cnsu_pkey.
         return.

   end.  /* for last cnsu_mstr */
end.  /* if io_batch <> 0 */
else do:

   /* FIND THE LAST "UNEXPORTED" USAGE RECORD TO GET THE BATCH NUMBER */
   for last cnsu_mstr where
        cnsu_po_nbr   = cnsix_mstr.cnsix_po_nbr   and
        cnsu_export_flag = no
   no-lock: end.

   if available (cnsu_mstr) then
      io_batch  = cnsu_batch.
   else do:
      /* GENERATE THE NEXT BATCH NUMBER */
      {mfrnseq.i cnsu_mstr io_batch cnsu_sq02}
   end.
end.  /* else do */

for first tr_hist no-lock where
   tr_trnbr = in_woiss_trnbr:
end.

/* CREATE NEW RECORD */
create cnsu_mstr.
assign
   cnsu_batch       = io_batch
   cnsu_trans_date  = today
   cnsu_po_nbr      = cnsix_mstr.cnsix_po_nbr
   cnsu_pod_line    = cnsix_mstr.cnsix_pod_line
   cnsu_part        = cnsix_mstr.cnsix_part
   cnsu_site        = cnsix_mstr.cnsix_site
   cnsu_qty_used    = in_qty_consumed
   cnsu_cum_qty     = in_qty_consumed + in_cum_qty
   cnsu_lotser      = cnsix_mstr.cnsix_lotser
   cnsu_ref         = cnsix_mstr.cnsix_ref
   cnsu_trnbr       = in_woiss_trnbr
   cnsu_print_flag  = no
   cnsu_export_flag = no
   cnsu_eff_date    = ip_effdate
   cnsu_mod_userid  = global_userid
   cnsu_mod_date    = today.

/* Get the primary key value */
{mfrnseq.i cnsu_mstr cnsu_mstr.cnsu_pkey cnsu_sq01}

out_pkey = cnsu_pkey.


END PROCEDURE.  /* Procedure createUsageMaster */


/* ========================================================================= */
PROCEDURE createUsageDetail:
/* ------------------------------------------------------------------------- */
define input parameter in_qty_consumed like remaining_xfer_qty no-undo.
define input parameter in_cnsu_pkey    like cnsu_pkey          no-undo.

/* DETERMINE IF USAGE DETAIL EXISTS */
for first cnsud_det where
   cnsud_cnsu_pkey  = in_cnsu_pkey          and
   cnsud_cnsix_pkey = cnsix_mstr.cnsix_pkey
exclusive-lock:
      cnsud_qty_used = cnsud_qty_used + in_qty_consumed.
end.  /* for first cnsud_det */

if not available (cnsud_det) then do:

   create cnsud_det.
   assign
      cnsud_cnsu_pkey      = in_cnsu_pkey
      cnsud_cnsix_pkey     = cnsix_mstr.cnsix_pkey
      cnsud_qty_used       = in_qty_consumed
      cnsud_receipt_trnbr  = cnsix_mstr.cnsix_receipt_trnbr
      cnsud_receiver       = cnsix_mstr.cnsix_receiver
      cnsud_asn_shipper    = cnsix_mstr.cnsix_asn_shipper
      cnsud_aged_date      = cnsix_mstr.cnsix_aged_date
      cnsud_orig_aged_date = cnsix_mstr.cnsix_orig_aged_date
      cnsud_mod_userid     = global_userid
      cnsud_mod_date       = today.

   /* ORACLE STD */
   if recid (cnsud_det) = -1 then .

end.  /* if not available (cnsud_det)  */

END PROCEDURE.  /* Procedure createUsageDetail */


/* ========================================================================= */
PROCEDURE createPendingVoucherRecords:
/* ------------------------------------------------------------------------- */
define input parameter ip_qty_consumed like remaining_xfer_qty no-undo.
define input parameter ip_eff_date as date no-undo.
define input parameter ip_usage_id like cnsu_batch no-undo.

/* CREATE A PENDING VOUCHER RECORD*/
/* BY POPULATING A TEMP-TABLE AND */
/* CALLING APPVROP.P TO CREATE THE PHYSICAL RECORD.*/

for first po_mstr
   where po_nbr = cnsix_mstr.cnsix_po_nbr
   no-lock:
end.

/* DETERMINE IF OPEN PENDING VOUCHER EXISTS */
if can-find(first pvo_mstr where
   pvo_order_type        = {&TYPE_PO}                and
   pvo_order             = cnsix_mstr.cnsix_po_nbr   and
   pvo_internal_ref_type = {&TYPE_POReceiver}        and
   pvo_internal_ref      = cnsix_mstr.cnsix_receiver and
   pvo_line              = cnsix_mstr.cnsix_pod_line and
   pvo_lc_charge         = ""                        and
   pvo_external_ref      = string(ip_usage_id)       and
   pvo_last_voucher      = "")
then do:

   /* LOAD TEMP-TABLE KEYS */
   create tt_pvo_mstr.
   assign
      tt_pvo_mstr.pvo_order_type         = {&TYPE_PO}
      tt_pvo_mstr.pvo_order              = cnsix_mstr.cnsix_po_nbr
      tt_pvo_mstr.pvo_internal_ref_type  = {&TYPE_POReceiver}
      tt_pvo_mstr.pvo_internal_ref       = cnsix_mstr.cnsix_receiver
      tt_pvo_mstr.pvo_line               = cnsix_mstr.cnsix_pod_line
      tt_pvo_mstr.pvo_lc_charge          = ""
      tt_pvo_mstr.pvo_external_ref       = string(ip_usage_id)
      proc_id                            = "read-open".

   if recid(tt_pvo_mstr) = -1 then.

   /* GET PENDING VOUCHER */
   {gprun.i ""appvorop.p""
      "(input proc_id,
        input-output table tt_pvo_mstr)"}

   /* READ THE PENDING VOUCHER TEMP TABLE RECORD AGAIN */
   for first tt_pvo_mstr:
   end.

   /* UPDATE PENDING VOUCHER */
   assign
      proc_id                   = "update-by-id"
      tt_pvo_mstr.pvo_trans_qty = tt_pvo_mstr.pvo_trans_qty + ip_qty_consumed.

   {gprun.i ""appvorop.p""
      "(input proc_id,
        input-output table tt_pvo_mstr)"}

end.  /* if can-find(first pvo_mstr where)  */
else do:

   /* LOAD TEMP-TABLE */
   create tt_pvo_mstr.

   assign
      tt_pvo_mstr.pvo_id                = 0
      tt_pvo_mstr.pvo_buyer             = po_buyer
      tt_pvo_mstr.pvo_lc_charge         = ""
      tt_pvo_mstr.pvo_supplier          = po_vend
      tt_pvo_mstr.pvo_internal_ref_type = {&TYPE_POReceiver}
      tt_pvo_mstr.pvo_internal_ref      = cnsix_mstr.cnsix_receiver
      tt_pvo_mstr.pvo_external_ref      = string(ip_usage_id)
      tt_pvo_mstr.pvo_trans_date        = ip_eff_date
      tt_pvo_mstr.pvo_eff_date          = ip_eff_date
      tt_pvo_mstr.pvo_trans_date        = ip_eff_date
      tt_pvo_mstr.pvo_last_voucher      = ""
      tt_pvo_mstr.pvo_ers_status        = 0
      tt_pvo_mstr.pvo_ers_opt           = integer(po_ers_opt)
      tt_pvo_mstr.pvo_ers_price_lst_opt = po_pr_lst_tp
      tt_pvo_mstr.pvo_shipfrom          = cnsix_mstr.cnsix_site
      tt_pvo_mstr.pvo_shipto            = po_ship
      tt_pvo_mstr.pvo_order_type        = {&TYPE_PO}
      tt_pvo_mstr.pvo_order             = cnsix_mstr.cnsix_po_nbr
      tt_pvo_mstr.pvo_line              = cnsix_mstr.cnsix_pod_line
      tt_pvo_mstr.pvo_part              = pod_det.pod_part
      tt_pvo_mstr.pvo_trans_qty         = ip_qty_consumed
      tt_pvo_mstr.pvo_curr              = po_curr
      tt_pvo_mstr.pvo_ex_rate           = exch_rate
      tt_pvo_mstr.pvo_ex_rate2          = exch_rate2
      tt_pvo_mstr.pvo_ex_ratetype       = exch_ratetype
      tt_pvo_mstr.pvo_exru_seq          = exch_exru_seq
      tt_pvo_mstr.pvo_accrual_acct      = pod_det.pod_acct
      tt_pvo_mstr.pvo_accrual_sub       = pod_det.pod_sub
      tt_pvo_mstr.pvo_accrual_cc        = pod_det.pod_cc
      tt_pvo_mstr.pvo_project           = pod_det.pod_project
      tt_pvo_mstr.pvo_taxable           = pod_det.pod_taxable
      tt_pvo_mstr.pvo_taxc              = pod_det.pod_taxc
      tt_pvo_mstr.pvo_tax_env           = pod_det.pod_tax_env
      tt_pvo_mstr.pvo_tax_in            = pod_det.pod_tax_in
      tt_pvo_mstr.pvo_tax_usage         = pod_det.pod_tax_usage
      tt_pvo_mstr.pvo_approver          = pod_det.pod_approve
      tt_pvo_mstr.pvo_consignment       = yes
      tt_pvo_mstr.pvo_mod_userid        = global_userid
      tt_pvo_mstr.pvo_mod_date          = today
      proc_id                           = "create".
   if recid(tt_pvo_mstr) = -1 then.


   /* CREATE PENDING VOUCHER */
   {gprun.i ""appvorop.p""
      "(input proc_id,
        input-output table tt_pvo_mstr)"}

end.  /* else do */


/* MULTI-DATABASE SUPPORT */
assign old_db = global_db.
if pod_det.pod_po_db <> "" and pod_po_db <> global_db then do:
   new_db = pod_det.pod_po_db.
   {gprun.i ""gpaliasd.p""}

   /* Need to copy usage records, but mc-copy-ex-rate-usage doesn't */
   /* work across databases, so the following is necessary.         */
   /* Clean out temp table and copy all exru_usage to it, for use   */
   /* later in poporcb1. */

   for each tt_exru_usage exclusive-lock:
      delete tt_exru_usage.
   end. /* for each tt_exru_usage */

   for each exru_usage
      where exru_usage.exru_seq = tt_pvo_mstr.pvo_exru_seq
   no-lock:
      buffer-copy exru_usage to tt_exru_usage.
      if recid(tt_exru_usage) = -1 then.
   end. /* for each exru_usage */

   /* COPY PVO_MSTR TO THE PO DB */
   {gprun.i ""ictranc1.p""
            "(input table tt_pvo_mstr)"}

   new_db = old_db.
   {gprun.i ""gpaliasd.p""}

   /* READ THE PENDING VOUCHER TEMP TABLE RECORD AGAIN */
   for first tt_pvo_mstr:
   end.

   /* SET THE LAST VOUCHER SO IT WON'T BE PICKED UP BY VOUCHER MAINT. */
   /* OR ERS.  USE THE DATABASE NAME AS A REFERENCE.                  */
   assign
      proc_id = "update-by-id"
      tt_pvo_mstr.pvo_last_voucher = pod_po_db.

   /* UPDATE THE PENDING VOUCHER */
   {gprun.i ""appvorop.p""
      "(input proc_id,
        input-output table tt_pvo_mstr)"}

end.  /* if pod_po_db <> "" and pod_po_db <> global_db */

END PROCEDURE.  /* Procedure createPendingVoucherRecords */


/* ========================================================================= */
PROCEDURE createConsignIssueTransaction:
/* ------------------------------------------------------------------------- */

   /*Calls ictrans.p to create a cn-iss tr_hist record */

   define input parameter ip_wo_nbr        like wo_nbr      no-undo.
   define input parameter ip_wo_lot        like wo_lot      no-undo.
   define input parameter ip_part          like pt_part     no-undo.
   define input parameter ip_wod_op        like wod_op      no-undo.
   define input parameter ip_wo_so_job     like wo_so_job   no-undo.
   define input parameter ip_site          like si_site     no-undo.
   define input parameter ip_loc           like pt_loc      no-undo.
   define input parameter ip_lotser        like sod_serial  no-undo.
   define input parameter ip_lotref        like sr_ref      no-undo.
   define input parameter ip_qty_req       like tr_qty_req  no-undo.
   define input parameter ip_qty_issued    like tr_qty_req  no-undo.
   define input parameter ip_offset_acct   like gl_inv_acct no-undo.
   define input parameter ip_offset_sub    like gl_inv_sub  no-undo.
   define input parameter ip_offset_cc     like gl_inv_cc   no-undo.
   define input parameter ip_cons_inv_acct like gl_inv_acct no-undo.
   define input parameter ip_cons_inv_sub  like gl_inv_sub  no-undo.
   define input parameter ip_cons_inv_cc   like gl_inv_cc   no-undo.
   define input parameter ip_effdate       like glt_effdate no-undo.
   define input parameter ip_price         like sod_price   no-undo.
   define input parameter ip_transtype     as character     no-undo.
   define input parameter ip_amt           as decimal       no-undo.
   define input parameter ip_trnbr         like tr_trnbr    no-undo.
   define output parameter op_cniss_trnbr  like tr_trnbr    no-undo.

   define variable ref like glt_ref no-undo.
   define variable amt as decimal   no-undo.

   find first gl_ctrl no-lock.

   if ip_wo_issue then do:

      for first wo_mstr
         where wo_nbr = ip_wo_nbr and
               wo_lot = ip_wo_lot
         no-lock:
      end.

      for first wod_det
         where
         wod_part = ip_part     and
         wod_lot  = ip_wo_lot   and
         wod_op   = ip_wod_op no-lock:
      end.

      /* CREATE USAGE tr_hist */
      {gprun.i
         ""ictrans.p""
         "(input wo_vend,
           input 0,
           input ip_cons_inv_acct,
           input ip_cons_inv_sub,
           input ip_cons_inv_cc,
           input wo_project,
           input """",
           input ip_offset_acct,
           input ip_offset_sub,
           input ip_offset_cc,
           input wo_project,
           input ip_effdate,
           input 0,
           input 0,
           input """",
           input 0,
           input ip_amt,
           input 0,
           input ip_loc,
           input ip_wo_lot,
           input ip_lotref,
           input ip_lotser,
           input 0,
           input ip_wo_nbr,
           input 0,
           input 0,
           input ip_part,
           input ?,
           input ip_price,
           input ip_qty_req,
           input wod_bo_chg,
           input ip_qty_issued,
           input """",
           input ip_trnbr,
           input """",
           input ip_site,
           input """",
           input """",
           input wo_so_job,
           input 0,
           input ip_transtype,
           input 0,
           input ip_site,
           output op_cniss_trnbr)"}

   end. /* if ip_wo_issue */
   else do:

      /* CREATE USAGE tr_hist */
      {gprun.i
         ""ictrans.p""
         "(input """",
           input 0,
           input ip_cons_inv_acct,
           input ip_cons_inv_sub,
           input ip_cons_inv_cc,
           input """",
           input """",
           input ip_offset_acct,
           input ip_offset_sub,
           input ip_offset_cc,
           input """",
           input ip_effdate,
           input 0,
           input 0,
           input """",
           input 0,
           input ip_amt,
           input 0,
           input ip_loc,
           input ip_wo_lot,
           input ip_lotref,
           input ip_lotser,
           input 0,
           input ip_wo_nbr,
           input 0,
           input 0,
           input ip_part,
           input ?,
           input ip_price,
           input ip_qty_req,
           input 0,
           input ip_qty_issued,
           input """",
           input ip_trnbr,
           input """",
           input ip_site,
           input """",
           input """",
           input ip_wo_so_job,
           input 0,
           input ip_transtype,
           input 0,
           input ip_site,
           output op_cniss_trnbr)"}

   end. /* ELSE DO */
END PROCEDURE.  /* Procedure createConsignIssueTransaction */


/* ========================================================================= */
PROCEDURE getAccounts:
/* ------------------------------------------------------------------------- */
   define input  parameter ip_part      like pt_part     no-undo.
   define input  parameter ip_site      like si_site     no-undo.
   define input  parameter ip_loc       like loc_loc     no-undo.
   define output parameter op_inv_acct  like gl_inv_acct no-undo.
   define output parameter op_inv_sub   like gl_inv_sub  no-undo.
   define output parameter op_inv_cc    like gl_inv_cc   no-undo.
   define output parameter op_rcpt_acct like gl_inv_acct no-undo.
   define output parameter op_rcpt_sub  like gl_inv_sub  no-undo.
   define output parameter op_rcpt_cc   like gl_inv_cc   no-undo.

   for first pt_mstr
      where pt_part = ip_part no-lock:
   end.


   for first pl_mstr
      where pl_prod_line = pt_prod_line no-lock:
   end. /* FOR FIRST PL_MSTR  */

   assign
      op_inv_acct  = pl_inv_acct
      op_inv_sub   = pl_inv_sub
      op_inv_cc    = pl_inv_cc
      op_rcpt_acct = pl_rcpt_acct
      op_rcpt_sub  = pl_rcpt_sub
      op_rcpt_cc   = pl_rcpt_cc.


   for first pld_det
      where pld_prodline = pt_prod_line
      and   pld_site     = ip_site
      and   pld_loc      = ip_loc
      no-lock:
   end. /* FOR FIRST PLD_DET */

   if not available pld_det then do:

      for first pld_det
         where pld_prodline = pt_prod_line
         and   pld_site     = ip_site
         and   pld_loc      = "" no-lock:
      end. /* FOR FIRST PLD_DET */

      if not available pld_det then do:

         for first pld_det
            where pld_prodline = pt_prod_line
            and   pld_site     = ""
            and   pld_loc      = "" no-lock:
         end. /* FOR FIRST PLD_DET */

      end.
  end.

  if available pld_det then
     assign
      op_inv_acct  = pld_inv_acct
      op_inv_sub   = pld_inv_sub
      op_inv_cc    = pld_inv_cc.

END PROCEDURE.  /* Procedure getAccounts*/


/* ========================================================================= */
PROCEDURE updateConsignmentInventory:
/* ------------------------------------------------------------------------- */
define input parameter transfer_qty like tr_qty_req no-undo.
define input parameter part like pt_part no-undo.
define input parameter site like si_site no-undo.
define input parameter loc like loc_loc no-undo.
define input parameter lotser like sr_lotser no-undo.
define input parameter lotref like sr_ref no-undo.

define variable supp_consign_qty_oh like ld_qty_oh no-undo.
define variable supp_consign_ld_oh like ld_qty_oh no-undo.
define variable procid as character no-undo.

procid = "update".
/* UPDATE THE INVENTORY MASTER CONSIGNMENT QOH BALANCE */
{gprun.i ""pocnin.p""
         "(input procid,
           input site,
           input part,
           input - transfer_qty,
           output supp_consign_qty_oh)"}


/* UPDATE THE LOCATION DETAIL CONSIGNMENT QOH BALANCE */
{gprun.i ""pocnld.p""
          "(input procid,
            input site,
            input part,
            input loc,
            input lotser,
            input lotref,
            input - transfer_qty,
            output supp_consign_ld_oh)"}

END PROCEDURE.  /* Procedure updateConsignmentInventory */
